﻿/**
*
*  RSSReader is a lightweight RSS client written in C#
*
*  Copyright 2012 Ashot Aslanyan <ashot.aslanian@gmail.com>
*
*  This file is part of RSSReader.
*
*  RSSReader is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  RSSReader is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with servmonitor.  If not, see <http://www.gnu.org/licenses/>.
*
*/

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace RSSReader
{
    /// <summary>
    /// Main form class
    /// </summary>
    public partial class FormView : Form, IView
    {
        private delegate void RefreshTree(bool fromFile);

        /// <summary>
        /// <see cref="System.Collections.Generic.List{T}"/> that contains feeds
        /// </summary>
        public List<RSSObject> RSSFeed { get; set; }

        /// <summary>
        /// Constructor for form
        /// </summary>
        public FormView()
        {
            this.RSSFeed = new List<RSSObject>();
            RSSReader.NewItem += new RSSReader.NewItemHandler(RSSReader_NewItem);

            this.InitializeComponent();
        }

        public void RSSReader_NewItem(string feedname, int newquanity)
        {
            Logger.Instance.Log("FormView", "new items!");
            this.NotificationIcon.BalloonTipText = String.Format("You have got {0} new items in {1}.", newquanity, feedname);
            this.NotificationIcon.ShowBalloonTip(500);
        }

        /// <summary>
        /// Refreshes feed tree.
        /// </summary>
        /// <param name="fromFile">Re-read file or not.</param>
        private void RefreshTreeView(bool fromFile)
        {
            System.Drawing.Font boldFont = new System.Drawing.Font(this.Items.Font, System.Drawing.FontStyle.Bold);

            Logger.Instance.Log("FormView", "clearing the list.");
            this.Items.Nodes.Clear();
            Logger.Instance.Log("FormView", "cleared");

            if (fromFile)
            {
                foreach (RSSObject obj in this.RSSFeed)
                {
                    obj.items = null;

                    obj.items = RSSReader.ToRssItemList(RSSReader.ReadFromFile(obj.filename));
                }
            }

            if (this.RSSFeed.Count > 0)
            {
                Logger.Instance.Log("FormView", "creating new treenode for each rss feed...");
                foreach (RSSObject obj in this.RSSFeed)
                {
                    TreeNode[] treeNodeItems = new TreeNode[obj.items.Count];

                    for (int i = 0; i < obj.items.Count; i++)
                    {
                        treeNodeItems[i] = new TreeNode(obj.items[i].title + String.Format(" [{0}]", RSSReader.getDateTime(obj.items[i].pubDate)));
                        if (obj.items[i].isRead == false)
                            treeNodeItems[i].NodeFont = boldFont;
                        else
                            treeNodeItems[i].NodeFont = new System.Drawing.Font(this.Items.Font, System.Drawing.FontStyle.Regular);
                    }

                    TreeNode feeds = new TreeNode("", treeNodeItems);
                    
                    if (obj.unreadItems > 0)
                    {
                        feeds.NodeFont = new System.Drawing.Font(this.Items.Font, System.Drawing.FontStyle.Bold);
                        Logger.Instance.Log("FormView", String.Format("{0} unread items: {1}", obj.title, obj.unreadItems));
                        feeds.Text = String.Format("{0} ({1})", obj.title, obj.unreadItems);
                    }
                    else
                    {
                        feeds.NodeFont = new System.Drawing.Font(this.Items.Font, System.Drawing.FontStyle.Regular);
                        feeds.Text = obj.title;
                    }

                    this.Items.Nodes.Add(feeds);
                }
                Logger.Instance.Log("FormView", "created.");

                Logger.Instance.Log("FormView", "setting right side content to first feeds first item.");
                this.Message.DocumentText = this.RSSFeed[0].items[0].description;
                Logger.Instance.Log("FormView", "set.");
            }
        }

        /// <summary>
        /// Get feeds URLs from config file.
        /// </summary>
        private void GetFeeds()
        {
            List<string> feedURLs = new List<string>();

            try
            {
                RSSReaderApplication.GetFeedURLs(ref feedURLs);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                Application.Exit();
            }


            if (feedURLs.Count > 0)
            {
                try
                {
                    foreach (string feedURL in feedURLs)
                    {
                        if (feedURL.Length > 0)
                        {
                            this.RSSFeed.Add(RSSReader.getFeed(new Uri(feedURL)));
                            Logger.Instance.Log("FormView", String.Format("{0} added from configuration file.", feedURL));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Instance.Log("FormView", String.Format("error while adding feeds."));
                    Logger.Instance.Log("FormView", String.Format("exception message was: {0}. \n Stack trace: {1}\n", ex.Message, ex.StackTrace));
                }

            }
        }

        /// <summary>
        /// Shows form
        /// </summary>
        public void ShowView()
        {
            Application.EnableVisualStyles();
            Application.Run(this);
        }

    }
}
